import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/common/styles/colors.dart';
import 'package:oramemo/widget/gradient_app_head.dart';
import 'package:oramemo/widget/gradient_background.dart';
import '../controllers/user_management_logic.dart';
import '../models/user.dart';
import '../widgets/search_bar.dart';
import '../widgets/user_card.dart';
import '../widgets/user_edit_dialog.dart';

class UserManagementPage extends StatelessWidget {
  const UserManagementPage({super.key});

  @override
  Widget build(BuildContext context) {
    final logic = Get.put(UserManagementLogic());

    return Scaffold(
      body: Column(
        children: [
          GradientAppHead(
            title: TextKey.whitelistTitle.tr,
            actions: TextButton(
              onPressed: () async {
                final result = await Get.dialog<User>(
                  const UserEditDialog(),
                );
                if (result != null) {
                  await logic.addUser(result);
                }
              },
              child: Text(
                TextKey.addWhitelist.tr,
                style: const TextStyle(fontSize: 16, color: Colours.text),
              ),
            ),
          ),
          Expanded(
            child: GradientBackground(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Column(
                  children: [
                    UserSearchBar(
                      onSearch: logic.searchUsers,
                    ),
                    const SizedBox(height: 10),
                    Expanded(
                      child: Obx(() => ListView.builder(
                            padding: EdgeInsets.zero,
                            itemCount: logic.filteredUsers.length,
                            itemBuilder: (context, index) {
                              final user = logic.filteredUsers[index];
                              return UserCard(
                                user: user,
                                onEdit: () async {
                                  final result = await Get.dialog<User>(
                                    UserEditDialog(user: user),
                                  );
                                  if (result != null) {
                                    await logic.editUser(result);
                                  }
                                },
                                onDelete: () async {
                                  final confirm = await Get.dialog<bool>(
                                    AlertDialog(
                                      title: Text(TextKey.confirmDelete.tr),
                                      content: Text(TextKey.confirmDeleteMessage.tr),
                                      actions: [
                                        TextButton(
                                          onPressed: () =>
                                              Get.back(result: false),
                                          child: Text(TextKey.cancel.tr),
                                        ),
                                        TextButton(
                                          onPressed: () =>
                                              Get.back(result: true),
                                          child: Text(TextKey.confirm.tr),
                                        ),
                                      ],
                                    ),
                                  );
                                  if (confirm == true) {
                                    await logic.deleteUser(user.id);
                                  }
                                },
                              );
                            },
                          )),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
